package walletuserslogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/app/rpc/model/pg/wallet"
	"navi-mumbai/common/key/walletkey"
	"navi-mumbai/common/utils"
	"navi-mumbai/common/xerr"

	"github.com/shopspring/decimal"
	"github.com/zeromicro/go-zero/core/stores/sqlx"

	"navi-mumbai/app/rpc/wallet/internal/svc"
	"navi-mumbai/app/rpc/wallet/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserCashOutLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserCashOutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserCashOutLogic {
	return &UserCashOutLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 用户cash out
func (l *UserCashOutLogic) UserCashOut(in *pb.UserCashOutReq) (*pb.UserCashOutResp, error) {
	defer utils.RecoverPanic()

	var err error
	var orderFlow *wallet.UserGameOrderFlow
	// 查询流水
	orderFlow, err = l.svcCtx.UserGameOrderFlowModel.FindOne(l.ctx, in.FlowNo)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}
	// 判断流水状态
	if orderFlow.State != walletkey.OrderFlowStateInit {
		l.Logger.Errorf("err == ", zap.Error(xerr.NewGrpcErrCodeMsg(xerr.WalletFlowStateNotAllow, "state error")))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.WalletFlowStateNotAllow, "state error")
	}
	// 查询钱包
	var uw *wallet.UserWallet
	uw, err = l.svcCtx.UserWalletModel.FindOneByUidMoneyType(l.ctx, in.Uid, in.MoneyType)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		if err == wallet.ErrNotFound {
			return nil, xerr.NewGrpcErrCodeMsg(xerr.WalletNotFound, "wallet not found")
		}
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}

	// 计算cash out金额
	var dWin decimal.Decimal
	dWin, err = decimal.NewFromString(in.Win)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.RequestParamError, err.Error())
	}

	var dOrderMoneyAmount = decimal.NewFromInt(orderFlow.MoneyAmount)

	var dCashOutAmount = dOrderMoneyAmount.Mul(dWin)

	iCashOutAmount := dCashOutAmount.RoundFloor(2).IntPart()

	err = l.svcCtx.UserWalletModel.Trans(l.ctx, func(ctx context.Context, tx sqlx.Session) error {
		// 更新钱包
		err2 := l.svcCtx.UserWalletModel.AddAmountTrans(ctx, tx, iCashOutAmount, uw.Id, uw.Uid, uw.MoneyType)
		if err2 != nil {
			l.Logger.Errorf("err == ", zap.Error(err2))
			return xerr.NewGrpcErrCodeMsg(xerr.DbError, err2.Error())
		}
		// 更新流水
		err2 = l.svcCtx.UserGameOrderFlowModel.UpdateCashOutTrans(ctx, tx, in.FlowNo, walletkey.OrderFlowStateCashOut, iCashOutAmount)
		if err2 != nil {
			l.Logger.Errorf("err == ", zap.Error(err2))
			return xerr.NewGrpcErrCodeMsg(xerr.DbError, err2.Error())
		}
		return nil
	})
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, err
	}

	var rsp pb.UserCashOutResp
	var dUwBalance = decimal.NewFromInt(uw.Amount)
	rsp.Balance = dUwBalance.Add(dCashOutAmount).Div(decimal.NewFromInt(walletkey.MoneyScale)).StringFixedBank(2)
	rsp.MoneyType = uw.MoneyType

	return &rsp, nil
}
